package com.cloudsigns.factorymanager2.engineering.model;

import com.cloudsigns.factorymanager2.MyApplicaton;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.bean.DispatchPatrolBean;
import com.cloudsigns.factorymanager2.engineering.bean.PatrolFeedbackBean;
import com.cloudsigns.factorymanager2.engineering.bean.PatrolOrderBean;
import com.cloudsigns.factorymanager2.engineering.utils.EngineeringUtil;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.ProjectUtil;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by tony on 2018/5/30.
 */

public class PatrolFeedBackModel implements IPatrolFeedBackModel {

    @Override
    public void patrolFeedBack(final PatrolFeedbackBean patrolFeedbackBean, final OnPatrolFeedBackListener patrolFeedBackListener) {
        new Thread(new Runnable() {
            public void run() {
                HttpClient client = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(ProjectUtil.PATROL_FEED_BACK);

                httpPost.addHeader(HTTP.CONTENT_TYPE, ConstantUtil.CONTENT_TYPE);
                httpPost.addHeader("token", MyApplicaton.getApplication().getToken());

                HttpResponse response;
                try {
                    JSONObject jsonObject = new JSONObject();
                    if (null != patrolFeedbackBean) {
                        jsonObject.put("bright", patrolFeedbackBean.getBright());
                        jsonObject.put("cable", patrolFeedbackBean.getCable());
                        jsonObject.put("current", patrolFeedbackBean.getCurrent());
                        List<String> environmentImgUrlList = patrolFeedbackBean.getEnvironmentImgUrlList();
                        if (null != environmentImgUrlList) {
                            JSONArray imgJsonArr = new JSONArray();
                            for (String imgurl : environmentImgUrlList) {
                                imgJsonArr.put(imgurl);
                            }

                            jsonObject.put("environmentImgUrlList", imgJsonArr);
                        }

                        jsonObject.put("orderId", patrolFeedbackBean.getId());
                        jsonObject.put("itemId", patrolFeedbackBean.getItemId());
                        CommonEnum.LIFTSYSTEM liftSystem = patrolFeedbackBean.getLiftSystem();
                        if (null != liftSystem) {
                            if (liftSystem == CommonEnum.LIFTSYSTEM.NORMAL) {
                                jsonObject.put("liftSystem", "NORMAL");
                            } else if (liftSystem == CommonEnum.LIFTSYSTEM.SHIELD) {
                                jsonObject.put("liftSystem", "SHIELD");
                            } else if (liftSystem == CommonEnum.LIFTSYSTEM.LEAK) {
                                jsonObject.put("liftSystem", "LEAK");
                            } else if (liftSystem == CommonEnum.LIFTSYSTEM.RUST) {
                                jsonObject.put("liftSystem", "RUST");
                            } else if (liftSystem == CommonEnum.LIFTSYSTEM.VARIANT) {
                                jsonObject.put("liftSystem", "VARIANT");
                            } else if (liftSystem == CommonEnum.LIFTSYSTEM.LOOSE) {
                                jsonObject.put("liftSystem", "LOOSE");
                            } else if (liftSystem == CommonEnum.LIFTSYSTEM.MALFUNCTION) {
                                jsonObject.put("liftSystem", "MALFUNCTION");
                            } else if (liftSystem == CommonEnum.LIFTSYSTEM.WEAR) {
                                jsonObject.put("liftSystem", "WEAR");
                            } else if (liftSystem == CommonEnum.LIFTSYSTEM.NOT_CLEAN) {
                                jsonObject.put("liftSystem", "NOT_CLEAN");
                            } else if (liftSystem == CommonEnum.LIFTSYSTEM.PLICATED) {
                                jsonObject.put("liftSystem", "PLICATED");
                            } else if (liftSystem == CommonEnum.LIFTSYSTEM.TEAR) {
                                jsonObject.put("liftSystem", "TEAR");
                            } else if (liftSystem == CommonEnum.LIFTSYSTEM.SCRATCH) {
                                jsonObject.put("liftSystem", "SCRATCH");
                            }
                        }

                        jsonObject.put("light", patrolFeedbackBean.getLight());
                        List<String> majorImgUrlList = patrolFeedbackBean.getMajorImgUrlList();
                        if (null != majorImgUrlList) {
                            JSONArray imgJsonArr = new JSONArray();
                            for (String imgurl : majorImgUrlList) {
                                imgJsonArr.put(imgurl);
                            }

                            jsonObject.put("majorImgUrlList", imgJsonArr);
                        }


                        CommonEnum.LIFTSYSTEM majorStructure = patrolFeedbackBean.getMajorStructure();
                        if (null != majorStructure) {
                            if (majorStructure == CommonEnum.LIFTSYSTEM.NORMAL) {
                                jsonObject.put("majorStructure", "NORMAL");
                            } else if (majorStructure == CommonEnum.LIFTSYSTEM.SHIELD) {
                                jsonObject.put("majorStructure", "SHIELD");
                            } else if (majorStructure == CommonEnum.LIFTSYSTEM.LEAK) {
                                jsonObject.put("majorStructure", "LEAK");
                            } else if (majorStructure == CommonEnum.LIFTSYSTEM.RUST) {
                                jsonObject.put("majorStructure", "RUST");
                            } else if (majorStructure == CommonEnum.LIFTSYSTEM.VARIANT) {
                                jsonObject.put("majorStructure", "VARIANT");
                            } else if (majorStructure == CommonEnum.LIFTSYSTEM.LOOSE) {
                                jsonObject.put("majorStructure", "LOOSE");
                            } else if (majorStructure == CommonEnum.LIFTSYSTEM.MALFUNCTION) {
                                jsonObject.put("majorStructure", "MALFUNCTION");
                            } else if (majorStructure == CommonEnum.LIFTSYSTEM.WEAR) {
                                jsonObject.put("majorStructure", "WEAR");
                            } else if (majorStructure == CommonEnum.LIFTSYSTEM.NOT_CLEAN) {
                                jsonObject.put("majorStructure", "NOT_CLEAN");
                            } else if (majorStructure == CommonEnum.LIFTSYSTEM.PLICATED) {
                                jsonObject.put("majorStructure", "PLICATED");
                            } else if (majorStructure == CommonEnum.LIFTSYSTEM.TEAR) {
                                jsonObject.put("majorStructure", "TEAR");
                            } else if (majorStructure == CommonEnum.LIFTSYSTEM.SCRATCH) {
                                jsonObject.put("majorStructure", "SCRATCH");
                            }
                        }

                        jsonObject.put("on_off", patrolFeedbackBean.getOn_off());

                        CommonEnum.LIFTSYSTEM outline = patrolFeedbackBean.getOutline();
                        if (null != outline) {
                            if (outline == CommonEnum.LIFTSYSTEM.NORMAL) {
                                jsonObject.put("outline", "NORMAL");
                            } else if (outline == CommonEnum.LIFTSYSTEM.SHIELD) {
                                jsonObject.put("outline", "SHIELD");
                            } else if (outline == CommonEnum.LIFTSYSTEM.LEAK) {
                                jsonObject.put("outline", "LEAK");
                            } else if (outline == CommonEnum.LIFTSYSTEM.RUST) {
                                jsonObject.put("outline", "RUST");
                            } else if (outline == CommonEnum.LIFTSYSTEM.VARIANT) {
                                jsonObject.put("outline", "VARIANT");
                            } else if (outline == CommonEnum.LIFTSYSTEM.LOOSE) {
                                jsonObject.put("outline", "LOOSE");
                            } else if (outline == CommonEnum.LIFTSYSTEM.MALFUNCTION) {
                                jsonObject.put("outline", "MALFUNCTION");
                            } else if (outline == CommonEnum.LIFTSYSTEM.WEAR) {
                                jsonObject.put("outline", "WEAR");
                            } else if (outline == CommonEnum.LIFTSYSTEM.NOT_CLEAN) {
                                jsonObject.put("outline", "NOT_CLEAN");
                            } else if (outline == CommonEnum.LIFTSYSTEM.PLICATED) {
                                jsonObject.put("outline", "PLICATED");
                            } else if (outline == CommonEnum.LIFTSYSTEM.TEAR) {
                                jsonObject.put("outline", "TEAR");
                            } else if (outline == CommonEnum.LIFTSYSTEM.SCRATCH) {
                                jsonObject.put("outline", "SCRATCH");
                            }
                        }

                        CommonEnum.LIFTSYSTEM picture = patrolFeedbackBean.getPicture();
                        if (null != picture) {
                            if (picture == CommonEnum.LIFTSYSTEM.NORMAL) {
                                jsonObject.put("picture", "NORMAL");
                            } else if (picture == CommonEnum.LIFTSYSTEM.SHIELD) {
                                jsonObject.put("picture", "SHIELD");
                            } else if (picture == CommonEnum.LIFTSYSTEM.LEAK) {
                                jsonObject.put("picture", "LEAK");
                            } else if (picture == CommonEnum.LIFTSYSTEM.RUST) {
                                jsonObject.put("picture", "RUST");
                            } else if (picture == CommonEnum.LIFTSYSTEM.VARIANT) {
                                jsonObject.put("picture", "VARIANT");
                            } else if (picture == CommonEnum.LIFTSYSTEM.LOOSE) {
                                jsonObject.put("picture", "LOOSE");
                            } else if (picture == CommonEnum.LIFTSYSTEM.MALFUNCTION) {
                                jsonObject.put("picture", "MALFUNCTION");
                            } else if (picture == CommonEnum.LIFTSYSTEM.WEAR) {
                                jsonObject.put("picture", "WEAR");
                            } else if (picture == CommonEnum.LIFTSYSTEM.NOT_CLEAN) {
                                jsonObject.put("picture", "NOT_CLEAN");
                            } else if (picture == CommonEnum.LIFTSYSTEM.PLICATED) {
                                jsonObject.put("picture", "PLICATED");
                            } else if (picture == CommonEnum.LIFTSYSTEM.TEAR) {
                                jsonObject.put("picture", "TEAR");
                            } else if (picture == CommonEnum.LIFTSYSTEM.SCRATCH) {
                                jsonObject.put("picture", "SCRATCH");
                            }
                        }

                        jsonObject.put("port", patrolFeedbackBean.getPort());
                        jsonObject.put("power", patrolFeedbackBean.getPower());

                        List<CommonEnum.PRESERVELIST> preserveList = patrolFeedbackBean.getPreserveList();
                        if (null != preserveList) {
                            JSONArray jsonArr = new JSONArray();
                            for (CommonEnum.PRESERVELIST preserve : preserveList) {
                                if (preserve == CommonEnum.PRESERVELIST.OUT_CLEAN) {
                                    jsonArr.put("OUT_CLEAN");
                                } else if (preserve == CommonEnum.PRESERVELIST.IN_CLEAN) {
                                    jsonArr.put("IN_CLEAN");
                                } else if (preserve == CommonEnum.PRESERVELIST.CABLE_PORT) {
                                    jsonArr.put("CABLE_PORT");
                                } else if (preserve == CommonEnum.PRESERVELIST.PLUG) {
                                    jsonArr.put("PLUG");
                                } else if (preserve == CommonEnum.PRESERVELIST.BOX_BOLT) {
                                    jsonArr.put("BOX_BOLT");
                                } else if (preserve == CommonEnum.PRESERVELIST.LIGHT_POWER) {
                                    jsonArr.put("LIGHT_POWER");
                                }
                            }

                            jsonObject.put("preserveList", jsonArr);
                        }

                        CommonEnum.LIFTSYSTEM pulley = patrolFeedbackBean.getPulley();
                        if (null != pulley) {
                            if (pulley == CommonEnum.LIFTSYSTEM.NORMAL) {
                                jsonObject.put("pulley", "NORMAL");
                            } else if (pulley == CommonEnum.LIFTSYSTEM.SHIELD) {
                                jsonObject.put("pulley", "SHIELD");
                            } else if (pulley == CommonEnum.LIFTSYSTEM.LEAK) {
                                jsonObject.put("pulley", "LEAK");
                            } else if (pulley == CommonEnum.LIFTSYSTEM.RUST) {
                                jsonObject.put("pulley", "RUST");
                            } else if (pulley == CommonEnum.LIFTSYSTEM.VARIANT) {
                                jsonObject.put("pulley", "VARIANT");
                            } else if (pulley == CommonEnum.LIFTSYSTEM.LOOSE) {
                                jsonObject.put("pulley", "LOOSE");
                            } else if (pulley == CommonEnum.LIFTSYSTEM.MALFUNCTION) {
                                jsonObject.put("pulley", "MALFUNCTION");
                            } else if (pulley == CommonEnum.LIFTSYSTEM.WEAR) {
                                jsonObject.put("pulley", "WEAR");
                            } else if (pulley == CommonEnum.LIFTSYSTEM.NOT_CLEAN) {
                                jsonObject.put("pulley", "NOT_CLEAN");
                            } else if (pulley == CommonEnum.LIFTSYSTEM.PLICATED) {
                                jsonObject.put("pulley", "PLICATED");
                            } else if (pulley == CommonEnum.LIFTSYSTEM.TEAR) {
                                jsonObject.put("pulley", "TEAR");
                            } else if (pulley == CommonEnum.LIFTSYSTEM.SCRATCH) {
                                jsonObject.put("pulley", "SCRATCH");
                            }
                        }

                        CommonEnum.LIFTSYSTEM safeLock = patrolFeedbackBean.getSafeLock();
                        if (null != safeLock) {
                            if (safeLock == CommonEnum.LIFTSYSTEM.NORMAL) {
                                jsonObject.put("safeLock", "NORMAL");
                            } else if (safeLock == CommonEnum.LIFTSYSTEM.SHIELD) {
                                jsonObject.put("safeLock", "SHIELD");
                            } else if (safeLock == CommonEnum.LIFTSYSTEM.LEAK) {
                                jsonObject.put("safeLock", "LEAK");
                            } else if (safeLock == CommonEnum.LIFTSYSTEM.RUST) {
                                jsonObject.put("safeLock", "RUST");
                            } else if (safeLock == CommonEnum.LIFTSYSTEM.VARIANT) {
                                jsonObject.put("safeLock", "VARIANT");
                            } else if (safeLock == CommonEnum.LIFTSYSTEM.LOOSE) {
                                jsonObject.put("safeLock", "LOOSE");
                            } else if (safeLock == CommonEnum.LIFTSYSTEM.MALFUNCTION) {
                                jsonObject.put("safeLock", "MALFUNCTION");
                            } else if (safeLock == CommonEnum.LIFTSYSTEM.WEAR) {
                                jsonObject.put("safeLock", "WEAR");
                            } else if (safeLock == CommonEnum.LIFTSYSTEM.NOT_CLEAN) {
                                jsonObject.put("safeLock", "NOT_CLEAN");
                            } else if (safeLock == CommonEnum.LIFTSYSTEM.PLICATED) {
                                jsonObject.put("safeLock", "PLICATED");
                            } else if (safeLock == CommonEnum.LIFTSYSTEM.TEAR) {
                                jsonObject.put("safeLock", "TEAR");
                            } else if (safeLock == CommonEnum.LIFTSYSTEM.SCRATCH) {
                                jsonObject.put("safeLock", "SCRATCH");
                            }
                        }


                        jsonObject.put("temperature", patrolFeedbackBean.getTemperature());

                        CommonEnum.EngineeringORderCategory type = patrolFeedbackBean.getOrderType();
                        if (null != type) {
                            if (type == CommonEnum.EngineeringORderCategory.INSTALL) {
                                jsonObject.put("type", "INSTALL");
                            } else if (type == CommonEnum.EngineeringORderCategory.BATCH_INSTALL) {
                                jsonObject.put("type", "BATCH_INSTALL");
                            } else if (type == CommonEnum.EngineeringORderCategory.CONSTRUCT) {
                                jsonObject.put("type", "CONSTRUCT");
                            } else if (type == CommonEnum.EngineeringORderCategory.MAINTAIN) {
                                jsonObject.put("type", "MAINTAIN");
                            } else if (type == CommonEnum.EngineeringORderCategory.PUBLICATION) {
                                jsonObject.put("type", "PUBLICATION");
                            } else if (type == CommonEnum.EngineeringORderCategory.PATROL) {
                                jsonObject.put("type", "PATROL");
                            }
                        }


                        CommonEnum.LIFTSYSTEM useEnvironment = patrolFeedbackBean.getUseEnvironment();
                        if (null != useEnvironment) {
                            if (useEnvironment == CommonEnum.LIFTSYSTEM.NORMAL) {
                                jsonObject.put("useEnvironment", "NORMAL");
                            } else if (useEnvironment == CommonEnum.LIFTSYSTEM.SHIELD) {
                                jsonObject.put("useEnvironment", "SHIELD");
                            } else if (useEnvironment == CommonEnum.LIFTSYSTEM.LEAK) {
                                jsonObject.put("useEnvironment", "LEAK");
                            } else if (useEnvironment == CommonEnum.LIFTSYSTEM.RUST) {
                                jsonObject.put("useEnvironment", "RUST");
                            } else if (useEnvironment == CommonEnum.LIFTSYSTEM.VARIANT) {
                                jsonObject.put("useEnvironment", "VARIANT");
                            } else if (useEnvironment == CommonEnum.LIFTSYSTEM.LOOSE) {
                                jsonObject.put("useEnvironment", "LOOSE");
                            } else if (useEnvironment == CommonEnum.LIFTSYSTEM.MALFUNCTION) {
                                jsonObject.put("useEnvironment", "MALFUNCTION");
                            } else if (useEnvironment == CommonEnum.LIFTSYSTEM.WEAR) {
                                jsonObject.put("useEnvironment", "WEAR");
                            } else if (useEnvironment == CommonEnum.LIFTSYSTEM.NOT_CLEAN) {
                                jsonObject.put("useEnvironment", "NOT_CLEAN");
                            } else if (useEnvironment == CommonEnum.LIFTSYSTEM.PLICATED) {
                                jsonObject.put("useEnvironment", "PLICATED");
                            } else if (useEnvironment == CommonEnum.LIFTSYSTEM.TEAR) {
                                jsonObject.put("useEnvironment", "TEAR");
                            } else if (useEnvironment == CommonEnum.LIFTSYSTEM.SCRATCH) {
                                jsonObject.put("useEnvironment", "SCRATCH");
                            }
                        }

                        jsonObject.put("voltage", patrolFeedbackBean.getVoltage());


                        CommonEnum.LIFTSYSTEM waterProofEnvironment = patrolFeedbackBean.getWaterProofEnvironment();
                        if (null != waterProofEnvironment) {
                            if (waterProofEnvironment == CommonEnum.LIFTSYSTEM.NORMAL) {
                                jsonObject.put("waterProofEnvironment", "NORMAL");
                            } else if (waterProofEnvironment == CommonEnum.LIFTSYSTEM.SHIELD) {
                                jsonObject.put("waterProofEnvironment", "SHIELD");
                            } else if (waterProofEnvironment == CommonEnum.LIFTSYSTEM.LEAK) {
                                jsonObject.put("waterProofEnvironment", "LEAK");
                            } else if (waterProofEnvironment == CommonEnum.LIFTSYSTEM.RUST) {
                                jsonObject.put("waterProofEnvironment", "RUST");
                            } else if (waterProofEnvironment == CommonEnum.LIFTSYSTEM.VARIANT) {
                                jsonObject.put("waterProofEnvironment", "VARIANT");
                            } else if (waterProofEnvironment == CommonEnum.LIFTSYSTEM.LOOSE) {
                                jsonObject.put("waterProofEnvironment", "LOOSE");
                            } else if (waterProofEnvironment == CommonEnum.LIFTSYSTEM.MALFUNCTION) {
                                jsonObject.put("waterProofEnvironment", "MALFUNCTION");
                            } else if (waterProofEnvironment == CommonEnum.LIFTSYSTEM.WEAR) {
                                jsonObject.put("waterProofEnvironment", "WEAR");
                            } else if (waterProofEnvironment == CommonEnum.LIFTSYSTEM.NOT_CLEAN) {
                                jsonObject.put("waterProofEnvironment", "NOT_CLEAN");
                            } else if (waterProofEnvironment == CommonEnum.LIFTSYSTEM.PLICATED) {
                                jsonObject.put("waterProofEnvironment", "PLICATED");
                            } else if (waterProofEnvironment == CommonEnum.LIFTSYSTEM.TEAR) {
                                jsonObject.put("waterProofEnvironment", "TEAR");
                            } else if (waterProofEnvironment == CommonEnum.LIFTSYSTEM.SCRATCH) {
                                jsonObject.put("waterProofEnvironment", "SCRATCH");
                            }
                        }


                    }

                    StringEntity se = new StringEntity(jsonObject.toString(), HTTP.UTF_8);


                    httpPost.setEntity(se);

                    response = client.execute(httpPost);
                    String result = EntityUtils.toString(response.getEntity(), "UTF-8").trim();
                    JSONObject json = new JSONObject(result);
                    String code = json.getString("code");
                    if (null != patrolFeedBackListener) {

                        if (code.equals(EngineeringUtil.HTTP_SUCCESSED_COD)) {
                            patrolFeedBackListener.patrolFeedBackSuccessed();
                        } else {
                            String error_str = code;
                            error_str += "," + ConstantUtil.getErrorCode(code);
                            if (!json.isNull("error_str")) {
                                error_str += "," + json.getString("error_str");
                            }


                            patrolFeedBackListener.patrolFeedBackFailed(error_str);


                        }

                    }

                } catch (Exception e) {
                    e.printStackTrace();
                } finally {

                }

            }
        }).start();
    }

    public interface OnPatrolFeedBackListener {
        void patrolFeedBackSuccessed();

        void patrolFeedBackFailed(String desc);
    }
}
